
# Create your views here.
from django.db.models import DateField, CharField
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render

from user.models import User
from.models import Vistor
def login_check(fn):
    def wrap(request,*args,**kwargs):
        if 'username'not in request.session or 'uid' not in request.session:
            return HttpResponseRedirect('/user/login')
        return fn(request,*args,**kwargs)
    return wrap

@login_check
def all_visitor(request):
    uid=request.session['uid']
    visitors=Vistor.objects.filter(user_id=uid)
    return render(request,'publish/all.html',locals())

@login_check
def add_visitor(request):
    if request.method=='GET':
        return render(request,'publish/add_visitor.html')
    elif request.method=='POST':
        uid=request.session['uid']
        title=request.POST.get('title')
        area=request.POST.get('area')
        dispute=request.POST.get('dispute')
        money_involved=request.POST.get('money_involved')
        content=request.POST.get('content')
        hope_time=request.POST.get('hope_time')
        username=request.POST.get('username')
        phone=request.POST.get('phone')
        print("************")
        print(title,area,dispute,money_involved,content,hope_time,username,phone)
        if not username or not phone :
            return HttpResponse('名字和电话不能为空')
        money_involved=float(money_involved)
        #         数据入库
        # try:
        Vistor.objects.create(title=title,
                                  area=area,
                                  dispute=dispute,
                                  money_involved=money_involved,
                                  content=content,
                                  hope_time=hope_time,
                                  username=username,
                                  phone=phone,
                                  user_id=uid)
        # except:
        #     return HttpResponse("您已发步信息")

        return HttpResponseRedirect('/publish/all_visitor')
        # return HttpResponse('NINL')

@login_check
def mod_visitor(request,vid):
    uid=request.session['uid']
    try:
        visitor=Vistor.objects.get(id=vid)
    except:
        return HttpResponse('您查的用户编号有误')
    if request.method=='GET':
        return render(request,'publish/mod_visitor.html',locals())
    elif request.method=='POST':
        username=request.POST.get('username')
        phone=request.POST.get('phone')
        title=request.POST.get('title')
        area=request.POST.get('area')
        dispute=request.POST.get('dispute')
        money_involved=request.POST.get('money_involved')
        content=request.POST.get('content')
        hope_time=request.POST.get('hope_time')
        print(username,title,phone,area,dispute,money_involved,content,hope_time)
        if not username or not phone:
            return HttpResponse('用户名和电话不能为空')
        # 修改数据
        visitor.username =username
        visitor.phone =phone
        visitor.title =title
        visitor.area =area
        visitor.dispute =dispute
        visitor.money_involved =money_involved
        visitor.content=content
        visitor.hope_time =hope_time

#         数据入库
        visitor.save()
    return HttpResponseRedirect('/publish/all_visitor')

@login_check
def del_visitor(request,vid):
    uid=request.session['uid']

    try:
        visitor=Vistor.objects.get(id=vid,user_id=uid)
        visitor.delete()
    except:
        return HttpResponse('用户编号有误')
    return HttpResponseRedirect('/publish/all_visitor')



